package com.example.fileserver_API.TimerTask;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.fileserver_API.Dao.SysappsettingMappers;
import com.example.fileserver_API.Dao.UploadpicturesMappers;
import com.example.fileserver_API.Entity.PO.sysappsettings;
import com.example.fileserver_API.Entity.PO.uploadpictures;
import com.example.fileserver_API.FixedValue.SetSettings;
import com.example.fileserver_API.utils.RedisUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 定时任务的java类
 */

@EnableScheduling   // 1.开启定时任务
@EnableAsync        // 2.开启多线程
@Component
public class TimerTask {

    @Resource
    SysappsettingMappers sysappsettingMappers;
    @Resource
    UploadpicturesMappers uploadpicturesMappers;
    @Resource
    RedisUtils redisUtils;

    /**
     * 同步数据库中的策略到redis中
     *
     * @throws Exception
     */
    @Async
    @Scheduled(fixedDelay = 1000 * 60 * 60)  //间隔1小时
    public void SetSettingToRedis() {
        QueryWrapper<sysappsettings> queryWrapper = new QueryWrapper<>();
        List<sysappsettings> articles = sysappsettingMappers.selectList(queryWrapper);
        for (sysappsettings item : articles) {
            if (item.getAppkey().equals(SetSettings.sys_watermarkimg)) {
                QueryWrapper<uploadpictures> uploadpicturesQueryWrapper = new QueryWrapper<>();
                uploadpicturesQueryWrapper.eq("guid", item.getAppvalue());
                uploadpictures obj = uploadpicturesMappers.selectOne(uploadpicturesQueryWrapper);
                System.out.println("开始同步系统设置到Redis（TimerTask=>SetSettingToRedis）：" + SetSettings.sys_syssetting + "=>" + SetSettings.sys_watermarkimgpath + "=>" + obj.getFilepath());
                redisUtils.SetHashValue(SetSettings.sys_syssetting, SetSettings.sys_watermarkimgpath, obj.getFilepath());
                redisUtils.SetHashValue(SetSettings.sys_syssetting, SetSettings.sys_watermarkimg, item.getAppvalue());
            } else {
                System.out.println("开始同步系统设置到Redis（TimerTask=>SetSettingToRedis）：" + SetSettings.sys_syssetting + "=>" + item.getAppkey() + "=>" + item.getAppvalue());
                redisUtils.SetHashValue(SetSettings.sys_syssetting, item.getAppkey(), item.getAppvalue());
            }
        }
    }
}
